package iosr11.gbase.dao;

import iosr11.data.ProjectGroup;
import iosr11.data.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.api.gbase.client.GoogleBaseFeed;
import com.google.api.gbase.client.GoogleBaseQuery;
import com.google.gdata.data.PlainTextConstruct;

public class StudentDAOImpl extends GenericDAOImpl<Student> implements StudentDAO{
	
	private static String TYPE = "System student";
	private static String NAME_INDEX = "index number";
	private static String NAME_USER_REF = "user";
	private static String PROJECT_GRP_REF = "project group";
	private Logger log = Logger.getLogger("service-log");

	@Override
	public Student getByUserRef(String ref) {
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault().getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance().getGoogleBaseQueryResult(query, NAME_USER_REF, ref);
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.warning("brak studenta o podanej referencji na uzytkownika");
			return null;
		}
		if (feed.getEntries().size()!=1) {
			log.warning("zla ilosc rekordow odnoszacych sie do jednego kontaktu (rozna od 1)");
			return null;
		}
		return this.rewrite(feed.getEntries().get(0));
	}
	
	

	@Override
	public List<Student> getByProjectGroup(ProjectGroup group) {
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault().getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance().getGoogleBaseQueryResult(query, PROJECT_GRP_REF, group.getId());
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.warning("brak studenta o podanej referencji na uzytkownika");
			return null;
		}
		if (feed.getEntries().size()<1) {
			log.warning("zla ilosc rekordow odnoszacych sie do grupy (siye < 1)");
			return null;
		}
		
		List<Student> result = new ArrayList<Student>();
		for (GoogleBaseEntry entry : feed.getEntries()){
			Student tmp = this.rewrite(entry);
			tmp.setProjectGrp(group);
			result.add(tmp);
		}
		return result;
	}



	@Override
	public List<Student> getAll() {
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault().getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance().getGoogleBaseQueryResult(query);
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.info("brak studentow w bazie danych");
			return null;
		}
		List<Student> result = new ArrayList<Student>();
		for (GoogleBaseEntry entry : feed.getEntries()){
			result.add(this.rewrite(entry));
		}
		log.info("znaleziono "+result.size()+" rekordow w bazie okreslajacych studentow");
		return result;
	}

	@Override
	public Student rewrite(GoogleBaseEntry entry) {
		Student student = new Student();
		student.setIndexNumber(entry.getGoogleBaseAttributes().getIntAttribute(NAME_INDEX));
		student.getUser().setId(entry.getGoogleBaseAttributes().getTextAttribute(NAME_USER_REF));
		student.setId(entry.getId());
		if (entry.getGoogleBaseAttributes().getTextAttribute(PROJECT_GRP_REF)!=null){
			if (entry.getGoogleBaseAttributes().getTextAttribute(PROJECT_GRP_REF).contains("http")){
				log.info("Przypisana grupa projektowa");
				ProjectGroup projectGrp = new ProjectGroup();
				projectGrp.setId(entry.getGoogleBaseAttributes().getTextAttribute(PROJECT_GRP_REF));
				student.setProjectGrp(projectGrp);
			}
			else {
				log.info("brak grupy projektowej");
				student.setProjectGrp(null);
			}
		}
		else 
			log.info("brak pola z grupa projektowa");
		return student;
	}

	@Override
	public GoogleBaseEntry rewrite(Student entity) {
		GoogleBaseEntry entry = new GoogleBaseEntry();
		entry.setTitle(new PlainTextConstruct("student"));
		entry.setDraft(true);
		entry.getGoogleBaseAttributes().setItemType(TYPE);
		entry.getGoogleBaseAttributes().addIntAttribute(NAME_INDEX, entity.getIndexNumber());
		entry.getGoogleBaseAttributes().addTextAttribute(NAME_USER_REF, entity.getUser().getId());
		if (entity.getProjectGrp()!=null){
			if (entity.getProjectGrp().getId()!=null){
				log.info("dodano grupe projektowa");
				entry.getGoogleBaseAttributes().addTextAttribute(PROJECT_GRP_REF, entity.getProjectGrp().getId());
				return entry;
			}
			log.info("niepoprawny id grupy");
		}
		log.info("brak grupy projektowej");
		entry.getGoogleBaseAttributes().addTextAttribute(PROJECT_GRP_REF, "null");
		return entry;
	}

	@Override
	public boolean checkType(GoogleBaseEntry entry) {
		return entry.getGoogleBaseAttributes().getItemType().equals(TYPE);
	}
	

}
